

document.addEventListener('DOMContentLoaded', function () {
    // 设置下拉菜单的 hover 事件
    setupHoverEvents('over_a1', 'over_a_1');
    setupHoverEvents('over_a2', 'over_a_2');

    // 鼠标移入背景颜色互换
    swapColors(document.getElementById('over_a1'), document.getElementById('over_a2'));

    // 初始化轮播图
    initCarousel();
});

// 设置下拉菜单的 hover 事件
function setupHoverEvents(mainId, subId) {
    const mainMenu = document.getElementById(mainId);
    const subMenu = document.getElementById(subId);

    mainMenu.addEventListener('mouseover', function () {
        subMenu.style.display = 'block';
        document.querySelectorAll('.left ul').forEach(otherSubMenu => {
            if (otherSubMenu !== subMenu) {
                otherSubMenu.style.display = 'none';
            }
        });
    });
}

// 鼠标移入背景颜色互换
function swapColors(l1, r1) {
    let swapped = false;
    l1.addEventListener('mouseover', () => {
        if (swapped) {
            l1.style.background = '#0175d4';
            r1.style.background = '#0B93FE';
        } else {
            l1.style.background = '#0B93FE';
            r1.style.background = '#0175d4';
        }
        swapped = !swapped;
    });
    r1.addEventListener('mouseover', () => {
        if (swapped) {
            l1.style.background = '#0175d4';
            r1.style.background = '#0B93FE';
        } else {
            l1.style.background = '#0B93FE';
            r1.style.background = '#0175d4';
        }
        swapped = !swapped;
    });
}

// 轮播图逻辑
function initCarousel() {
    const images = document.querySelectorAll('.carousel-img');
    const controls = document.querySelectorAll('.carousel-control');
    const links = document.querySelectorAll('.carousel-link');
    let currentIndex = 0;

    const updateCarousel = (index) => {
        images.forEach((img, i) => img.classList.toggle('active', i === index));
        controls.forEach((ctrl, i) => ctrl.classList.toggle('active', i === index));
        links.forEach((lnk, i) => lnk.classList.toggle('active', i === index));
        currentIndex = index;
    };

    controls.forEach((control, i) => {
        control.addEventListener('click', (e) => {
            e.preventDefault();
            updateCarousel(i);
        });
    });

    setInterval(() => {
        const nextIndex = (currentIndex + 1) % images.length;
        updateCarousel(nextIndex);
    }, 5000);

    // 初始化第一张图片和控制按钮为 active
    images[currentIndex].classList.add('active');
    controls[currentIndex].classList.add('active');
}

// 通过XMLHttpRequest模拟获取news数据
function loadNews() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://localhost:3000/news', true); // 新闻数据的API路径为 /news
    xhr.onload = function () {
        if (this.status === 200) {
            var newsTitles = JSON.parse(this.responseText); // 解析响应的JSON数据
            var newsList = document.getElementById('newsList'); // 获取新闻列表容器
            newsList.innerHTML = ''; // 清空新闻列表

            newsTitles.forEach(function (title) {
                var listItem = document.createElement('li'); // 创建新的列表项
                listItem.textContent = title; // 设置列表项的文本内容
                newsList.appendChild(listItem); // 将列表项添加到新闻列表容器中
            });
        } else {
            console.error('Failed to fetch news data: ' + this.status);
        }
    };
    xhr.onerror = function () {
        console.error('Request error...');
    };
    xhr.send();
}

document.addEventListener('DOMContentLoaded', function () {
    loadNews(); // 页面加载完成后，调用 loadNews 函数
});
